package com.cnfangmao.masterdata.controller;


import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.common.util.SnowFlakeUtils;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.dto.RolePermissionDto;
import com.cnfangmao.masterdata.dto.RolePermissionSearchDto;
import com.cnfangmao.masterdata.model.RolePermission;
import com.cnfangmao.masterdata.service.RolePermissionService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 角色权限关系表 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-17
 */
@RequestMapping("/rolePermission")
@Api(tags="0-5、配置-角色权限关系管理")
@RestController
public class RolePermissionController {

	private static final Logger logger =LoggerFactory.getLogger(RolePermissionController.class);
	
	@Autowired
	private RolePermissionService rolePermissionService;
	
	@PostMapping("/list.do")
	@ApiOperation(value="查找角色权限关系列表")
	@ApiImplicitParam(name="dto",value="查找角色权限关系列表条件",paramType="body",required=true,dataType="GeneralSearchDto«RolePermissionSearchDto»")
	public ResBody<GeneralPageVo<RolePermission>>  listRolePermission(@RequestBody GeneralSearchDto<RolePermissionSearchDto> dto) {
		ResBody<GeneralPageVo<RolePermission>>  resBody =new ResBody<GeneralPageVo<RolePermission>>();
		try {
			IPage<RolePermission> rolePermission =rolePermissionService.queryListRolePermission(dto);
			GeneralPageVo<RolePermission> result= new GeneralPageVo<RolePermission>();
			result.setPageNo(rolePermission.getCurrent());
			result.setPageSize(rolePermission.getPages());
			result.setRecords(rolePermission.getRecords());
			result.setTotalCount(rolePermission.getTotal());
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(result);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run list error in RolePermissionController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping(value="/create.do")
	@ApiOperation(value="创建角色权限关系信息")
	@ApiImplicitParam(name="dto",value="创建角色权限关系信息",paramType="body",required=true,dataType="RolePermissionDto")
	public ResBody<Boolean> createRolePermission(@RequestBody RolePermissionDto dto) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			if (dto.getRoleId().isEmpty() || dto.getPermissionId().isEmpty()) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("角色id或者权限id为空");
				return resBody;
			}
			if(rolePermissionService.findByRoleIdAndPermissionId(dto.getRoleId(), dto.getPermissionId())!=null)
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("角色权限关系已经存在");
				return resBody;
			}
			dto.setRolePermissionId(SnowFlakeUtils.generator());
			RolePermission rolePermission =ModelUtils.parseObject(dto, RolePermission.class, "create");
			rolePermissionService.save(rolePermission);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("创建成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run create error in RolePermissionController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;		
	}
	
	@PostMapping(value="/update.do")
	@ApiOperation(value="修改角色权限关系信息")
	@ApiImplicitParam(name="dto",value="修改角色权限关系信息",paramType="body",required=true,dataType="RolePermissionDto")
	public ResBody<Boolean> updateRolePermission(@RequestBody RolePermissionDto dto) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			if (dto.getRoleId().isEmpty() || dto.getPermissionId().isEmpty()) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("角色id或者权限id为空");
				return resBody;
			}
			RolePermission rolePermission =rolePermissionService.findByRoleIdAndPermissionId(dto.getRoleId(), dto.getPermissionId());
			if(rolePermission!=null && !rolePermission.getRolePermissionId().equals(dto.getRolePermissionId()))
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("角色权限关系已经存在");
				return resBody;
			}
			RolePermission entity =ModelUtils.parseObject(dto, RolePermission.class, "update");
			rolePermissionService.updateById(entity);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("修改成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run update error in RolePermissionController： %s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping(value="/delete.do")
	@ApiOperation(value="删除角色权限关系信息")
	@ApiImplicitParam(name="rolePermissionId",value="删除角色权限关系 rolePermissionId",paramType="body",required=true,dataType="string")
	public ResBody<Boolean> deleteRolePermission(@RequestBody String rolePermissionId) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			rolePermissionService.removeById(rolePermissionId);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("删除成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run delete error in RolePermissionController :%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping(value="/getRolePermission.do")
	@ApiOperation(value="获取角色权限关系信息")
	@ApiImplicitParam(name="rolePermissionId",value="获取角色权限关系 rolePermissionId",paramType="body",required=true,dataType="string")
	public ResBody<RolePermission> getRolePermission(@RequestBody String rolePermissionId) {
		ResBody<RolePermission> resBody =new ResBody<RolePermission>();
		try {
			RolePermission rolePermission =rolePermissionService.getById(rolePermissionId);
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
    		resBody.data(rolePermission);
		} catch (Exception e) {
			logger.error(String.format("run getRolePermission error in RolePermissionController :%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	 /**
     * 获取所有角色权限对应数据
     */
	@PostMapping(value="/getRolePermissionByRoleId.do")
    @ApiOperation(value="获取所有角色权限对应数据", notes = "")
    @ApiImplicitParam(name = "roleId", value = "角色roleId", paramType="body",required = true, dataType = "string")
    public ResBody<List<RolePermissionDto>> getRolePermissionByRoleId(@RequestBody String roleId){
		ResBody<List<RolePermissionDto>> resBody = new ResBody<List<RolePermissionDto>>();
    	try {
        	List<RolePermissionDto> rolePermission=rolePermissionService.getRolePermission(roleId);
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
    		resBody.data(rolePermission);
		} catch (Exception e) {
			logger.error(String.format("run getRolePermissionByRoleId error in RolePermissionController :%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	return resBody;
    }
}

